/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.SearchISBNUPCModel;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pojo.DimensionData;
import pojo.DimensionMedia;

/**
 *
 * @author A7med Kotb
 */
public class SearchISBNUPC extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String searchType = request.getParameter("searchtype");
        System.out.println("Search Type is : "+searchType);
        if (searchType != "0") {
            SearchISBNUPCModel ssm=new SearchISBNUPCModel(getDataSource(request, "MyDS").getConnection());
            StringBuffer result = new StringBuffer();
            String inputvalue = request.getParameter("inputvalue");
            System.out.println("Input Value is : "+inputvalue);
            if (searchType.equalsIgnoreCase("isbn")){
                long isbn = Long.parseLong(inputvalue);
                result.append(" <table border=\"1\">  <tr>  <th> ISBN13 </th> <th> Length </th> <th> LengthBy </th> <th> Width </th> <th> WidthBy </th> <th> Depth </th> <th> DepthBy </th> <th> Weight </th> <th> WeightBy </th> <th> Pages </th> <th> PagesBy </th> <th> Format </th> <th> FormatBy </th> <th> MailFormat </th> <th> MailFormatBy </th> <th> Items </th> <th> ItemsBy </th> <th> RecordUpdateDate </th></tr>");
                Vector<DimensionData> ddvect = ssm.getISBN(isbn);
                for (int i = 0; i < ddvect.size(); i++) {
                    result.append("<tr> <td> " + ddvect.get(i).getIsbn13() + " </td>   <td> " + ddvect.get(i).getLength() + " </td> "
                            + " <td>" + ddvect.get(i).getLengthBy() + " </td> <td>" + ddvect.get(i).getWidth() + " </td> <td>" + ddvect.get(i).getWidthBy() + " </td> <td>" + ddvect.get(i).getDepth() + " </td> <td>" + ddvect.get(i).getDepthBy() + " </td> <td>" + ddvect.get(i).getWeight() + " </td> <td>" + ddvect.get(i).getWeightBy() + " </td> <td>" + ddvect.get(i).getPages() + " </td><td>" + ddvect.get(i).getPagesBy() + " </td> <td>" + ddvect.get(i).getFormat() + " </td><td>" + ddvect.get(i).getFormatBy() + " </td> <td>" + ddvect.get(i).getMailFormat() + " </td><td>" + ddvect.get(i).getMailFormatBy() + " </td> <td>" + ddvect.get(i).getItems() + " </td><td>" + ddvect.get(i).getItemsBy() + " </td> <td>" + ddvect.get(i).getRecUpdateDate() + " </td></tr>");
                }
                result.append("</table>");
                response.getWriter().println(result.toString());
            } 
            else if (searchType.equalsIgnoreCase("upc")) {
                result.append(" <table border=\"1\">  <tr>  <th> UPC </th> <th> Length </th> <th> LengthBy </th> <th> Width </th> <th> WidthBy </th> <th> Depth </th> <th> DepthBy </th> <th> Weight </th> <th> WeightBy </th> <th> ProdCode </th> <th> ProdCodeBy </th> <th> MailFormat </th> <th> MailFormatBy </th> <th> Items </th> <th> ItemsBy </th></tr>");
                Vector<DimensionMedia> dmvect = ssm.getUPC(inputvalue);
                for (int i = 0; i < dmvect.size(); i++) {
                    result.append("<tr> <td> " + dmvect.get(i).getUpc() + " </td>   <td> " + dmvect.get(i).getLength() + " </td> "
                            + " <td>" + dmvect.get(i).getLengthBy() + " </td> <td>" + dmvect.get(i).getWidth() + " </td> <td>" + dmvect.get(i).getWidthBy() + " </td> <td>" + dmvect.get(i).getDepth() + " </td> <td>" + dmvect.get(i).getDepthBy() + " </td> <td>" + dmvect.get(i).getWeight() + " </td> <td>" + dmvect.get(i).getWeightBy() + " </td> <td>" + dmvect.get(i).getProdCode() + " </td><td>" + dmvect.get(i).getProdCodeBy() + " </td> <td>" + dmvect.get(i).getMailFormat() + " </td><td>" + dmvect.get(i).getMailFormatBy() + " </td> <td>" + dmvect.get(i).getItems() + " </td><td>" + dmvect.get(i).getItemsBy() + " </td> </tr>");
                }
                result.append("</table>");
                response.getWriter().println(result.toString());
            }

        } else {
            response.getWriter().println("No Data Found");
        }
        return null;
    }
}
